package com.milkshopbackend.milkshop.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.annotation.TableField;
import com.milkshopbackend.milkshop.entity.OrderItemDTO;
import lombok.Data;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@TableName("order_item")
public class OrderItem {

    @TableId(type = IdType.AUTO)
    private Long id;

    private Long orderId;
    private Long productId;
    @TableField("spec_type")
    private String specType;
    private Integer quantity;
    private BigDecimal price;
    private String productName;
    private String productImage;
    private String specName;
    private BigDecimal totalPrice;
    private LocalDateTime createTime;
    private LocalDateTime updateTime;

    public static OrderItem createFromProduct(Product product, OrderItemDTO orderItemDTO) {
        OrderItem orderItem = new OrderItem();
        orderItem.setProductId(product.getId());
        orderItem.setSpecType(orderItemDTO.getSpecType());
        orderItem.setProductName(product.getName());
        orderItem.setSpecName(product.getSpecType());
        orderItem.setPrice(product.getPrice());
        orderItem.setQuantity(orderItemDTO.getQuantity());
        orderItem.setTotalPrice(product.getPrice().multiply(new BigDecimal(orderItemDTO.getQuantity())));
        orderItem.setCreateTime(LocalDateTime.now());
        orderItem.setUpdateTime(LocalDateTime.now());

        return orderItem;
    }

    public String getSpecType() {
        return specType;
    }

    public void setSpecType(String specType) {
        this.specType = specType;
    }
}
